<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
            <el-form-item label="规格名称" prop="deptName">
                <el-input
                    v-model="queryParams.search"
                    placeholder="请输入仪表名称"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                >新增</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table  v-loading="loading" :data="goodsRuleLists" border fit highlight-current-row>
            <el-table-column type="selection" width="55" bo align="center" label-width="100px" />
            <el-table-column label="编号" align="center" prop="id"  width="50"/>
            <el-table-column label="规格名" align="center" prop="rule_name"/>
            <el-table-column label="商品规格" align="center" prop="attr_name"/>
            <el-table-column label="规格属性" align="center"  :show-overflow-tooltip="true" >
                <template slot-scope="scope">
                    <span
                        v-for="(item, index) in scope.row.attr_value"
                        :key="index"
                        v-text="item"
                        style="display: block"
                    ></span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
                <template slot-scope="{row}">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(row)"
                    >修改</el-button>
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(row)"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.page"
            :limit.sync="queryParams.limit"
            @pagination="getList"
        />
        <!-- 添加或修改部门对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-row :gutter="24">
                    <el-col :span="24">
                        <el-col :span="8">
                            <el-form-item label="规格模板名称：" prop="rule_name">
                                <el-input placeholder="请输入标题名称" :maxlength="20" v-model.trim="form.rule_name" />
                            </el-form-item>
                        </el-col>
                    </el-col>
                    <el-col :span="23" class="noForm" v-for="(item, index) in form.spec" :key="index">
                        <el-form-item>
                            <div class="acea-row row-middle">
                                <span class="mr5">{{ item.value }}</span>
                                <i class="el-icon-close" style="font-size: 14px" v-db-click @click="handleRemove(index)"></i>
                            </div>
                            <div class="rulesBox">
                                <el-tag
                                    class="mr14 mb10"
                                    closable
                                    color="primary"
                                    v-for="(j, indexn) in item.detail"
                                    :key="indexn"
                                    @close="handleRemove2(item.detail, indexn)"
                                >{{ j }}</el-tag
                                >
                                <el-input
                                    placeholder="请输入属性名称"
                                    v-model.trim="item.detail.attrsVal"
                                    @keyup.enter.native="createAttr(item.detail.attrsVal, index)"
                                    class="mb10 form_content_width"
                                >
                                    <template slot="append">
                                        <el-button type="primary" v-db-click @click="createAttr(item.detail.attrsVal, index)">确定</el-button>
                                    </template>
                                </el-input>
                            </div>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24" v-if="isBtn" class="mt10">
                        <el-col :span="8" class="mr15">
                            <el-form-item label="规格名称：">
                                <el-input placeholder="请输入规格" v-model="attrsName" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="8" class="mr20">
                            <el-form-item label="规格值：">
                                <el-input v-model="attrsVal" placeholder="请输入规格值" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="2">
                            <el-button type="primary" v-db-click @click="createAttrName">确定</el-button>
                        </el-col>
                        <el-col :span="2">
                            <el-button v-db-click @click="offAttrName">取消</el-button>
                        </el-col>
                    </el-col>
                </el-row>
                <el-button type="primary" v-db-click @click="addBtn" v-if="!isBtn" class="add">添加新规格</el-button>


<!--
                <el-form-item label="规格名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入规格名称" />
                </el-form-item>





                <el-form-item label="参数" prop="remark">
                    <el-button type="primary" icon="el-icon-plus" size="small" @click="addRule">新的规格</el-button>
                    <el-table
                        :data="preItem"
                        border
                        style="width: 100%">
                        <el-table-column
                            label="规格名"
                            width="200">
                            <template slot-scope="scope">
                                <el-input placeholder="规格名" style="width: 150px" v-model="scope.row.title"></el-input>
                                <i class="el-icon-delete del-btn" @click="delTitle(scope.$index)"></i>
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="规格值">
                            <template slot-scope="scope">
                                <div style="width: 180px;float: left;margin-top: 5px" v-for="(vo,index) in scope.row.item" :key="index">
                                    <el-input placeholder="规格值" style="width: 150px" v-model="scope.row.item[index]"></el-input>
                                    <i class="el-icon-delete del-btn" @click="delItem(scope.$index, index)"></i>
                                </div>
                                <i class="el-icon-plus add-btn" @click="addNewItem(scope.$index)">添加</i>
                            </template>
                        </el-table-column>
                    </el-table>


                </el-form-item>-->
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <!-- 检测项选择器 -->

    </div>
</template>
<script>
import {getGoodsRuleList,createGoodsRule,updateGoodsRule,detailGoodsRule,deleteGoodsRule} from "@/api/grzq/mall/goods_rule";

export default {
    name: "company",
    dicts:[],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 表格数据
            goodsRuleLists: [],
            itemList:[],

            total: 0,
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                search: undefined,
                status: undefined,
                page: 1,
                limit: 10,
            },
            ruleItem: {
                'title': '',
                'item': ['']
            },
            preItem: [],
            // 表单校验
            //////--------------------

            // 表单参数
            form: {
                rule_name: '',
                spec: [],
            },
            rules: {
                rule_name: [
                    { required: true, message: "规格名称不能为空", trigger: "blur" }
                ],
            },
            grid: {
                xl: 3,
                lg: 3,
                md: 12,
                sm: 24,
                xs: 24,
            },

            isBtn: false,
            attrsName: '',
            attrsVal: '',

        };
    },
    computed: {

    },
    created() {
        this.getList();
    },
    methods: {
        handleChange(value, direction, movedKeys) {
            console.log(value, direction, movedKeys);
        },
        /** 查询部门列表 */
        async getList() {
            let that = this;
            that.loading = true;
            const {data, status, msg} = await getGoodsRuleList(this.queryParams);
            that.goodsRuleLists = data.data;
            that.loading = false;
            that.total = data.total;
            that.queryParams.limit = data.per_page || 10;

        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: undefined,
                rule_name: undefined,
                spec: [],
            };
            this.resetForm("form");
        },

        /** 搜索按钮操作 */
        handleQuery() {
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleAdd(row) {
            this.reset();
            this.open = true;
            this.title = "添加商品规格";
            this.getAllCom()
        },

        // 添加规格
        addAttr() {
            this.title = '新增商品规格'
            this.dialogVisible = true
            this.preItem = [];
            this.preItem.push(JSON.parse(JSON.stringify(this.ruleItem)))
        },
        addRule() {
            let item = JSON.parse(JSON.stringify(this.ruleItem))
            this.preItem.push(item)
        },
        // 删除规格标题
        delTitle(index) {
            this.preItem.splice(index, 1);
        },
        // 删除规格
        delItem(tableIndex, index) {
            this.preItem[tableIndex].item.splice(index, 1);
        },
        // 添加新的节点
        addNewItem(index) {
            this.preItem[index].item.push('');
        },


        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.id
            detailGoodsRule(id).then(response => {
                console.log('this.form',response)
                const {data}= response;
                this.form = data;
                console.log('this.form',this.form)
                this.open = true;
                this.title = "修改";
            });
        },
        /** 提交按钮 */
        submitForm: function() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != undefined) {
                        updateGoodsRule(this.form).then(response => {
                            this.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        createGoodsRule(this.form).then(response => {
                            this.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        removeItem(index){

        },


        /** 删除按钮操作 */
        handleDelete(row) {
            this.confirm('是否确认删除名称为"' + row.meter_name + '"的数据项？').then(function() {
                return deleteGoodsRule({read_id:row.read_id});
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            }).catch(() => {});
        },

    //       *-----------------------------------
        // 添加按钮
        addBtn() {
            this.isBtn = true;
        },
        // 取消
        offAttrName() {
            this.isBtn = false;
        },
        // 删除
        handleRemove(index) {
            this.form.spec.splice(index, 1);
        },
        // 删除属性
        handleRemove2(item, index) {
            item.splice(index, 1);
        },
        // 添加规则名称
        createAttrName() {
            if (this.attrsName && this.attrsVal) {
                let data = {
                    value: this.attrsName,
                    detail: [this.attrsVal],
                };
                this.form.spec.push(data);
                var hash = {};
                this.form.spec = this.form.spec.reduce(function (item, next) {
                    /* eslint-disable */
                    console.log("item",item)
                    hash[next.value] ? '' : (hash[next.value] = true && item.push(next));
                    return item;
                }, []);
                this.attrsName = '';
                this.attrsVal = '';
                this.isBtn = false;
            } else {
                this.$message.warning('请添加规格名称或规格值');
            }
        },
        // 添加属性
        createAttr(num, idx) {
            if (num) {
                this.form.spec[idx].detail.push(num);
                var hash = {};
                this.form.spec[idx].detail = this.form.spec[idx].detail.reduce(function (item, next) {
                    /* eslint-disable */
                    hash[next] ? '' : (hash[next] = true && item.push(next));
                    return item;
                }, []);
            } else {
                this.$message.warning('请添加属性');
            }
        },

    }
};
</script>
